//
//  MYGCommunityQuestionCell.swift
//  MYGrow
//
//  Created by lance on 2024/3/2.
//

import UIKit
import QMUIKit
class MYGCommunityQuestionCell: UITableViewCell {
    // MARK: - Property
    var model: MYGCommunityCellModel? {
        didSet {
            guard let model = model else { return }
            questionLabel.text = model.title
            if model.content.count > 0 {
                doctorHeadImageView.sd_setImage(with: URL(string: model.headImageUrl), placeholderImage: UIImage(named: "head"))
                doctorInfoLabel.text = model.name
                answerLabel.text = model.content
            } else {
                doctorHeadImageView.image = headPlaceholderImage
                doctorInfoLabel.text = "暂无回答"
                answerLabel.text = ""
            }
            locationLabel.text = model.location == "" ? "未知" : model.location
            likeButton.setImage(model.isLike ? UIImage(named: "Community/like") : UIImage(named: "Community/dislike"), for: .normal)
            likeButton.setTitle("\(model.likeNum)", for: .normal)
        }
    }
    
    var commentAction: (() -> ())?
    var likeAction: (() -> ())?
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        contentView.backgroundColor = .white
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - UI
    private func configUI() {
        contentView.addSubview(cardView)
        cardView.addSubviews([
            questionIcon,
            questionLabel,
            answerCardView,
            locationIcon,
            locationLabel,
            likeButton,
        ])
        answerCardView.addSubviews([
            doctorHeadImageView,
            doctorInfoLabel,
            answerLabel
        ])
        
        cardView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 5, left: 15, bottom: 10, right: 15))
        }
        
        questionIcon.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 25, height: 25))
        }
        
        questionLabel.snp.makeConstraints { make in
            make.centerY.equalTo(questionIcon)
            make.left.equalTo(questionIcon.snp.right).offset(8)
            make.right.equalTo(-15)
            make.height.greaterThanOrEqualTo(22.5)
        }
        
        answerCardView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(15)
            make.top.equalTo(questionLabel.snp.bottom).offset(15)
            make.bottom.equalTo(-50)
        }
        
        doctorHeadImageView.snp.makeConstraints { make in
            make.left.top.equalTo(12.5)
            make.size.equalTo(CGSize(width: 25, height: 25))
        }
        
        doctorInfoLabel.snp.makeConstraints { make in
            make.centerY.equalTo(doctorHeadImageView)
            make.left.equalTo(doctorHeadImageView.snp.right).offset(10)
        }
        
        answerLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12.5)
            make.top.equalTo(doctorHeadImageView.snp.bottom).offset(7.5)
            make.bottom.equalTo(-15)
        }
        
        likeButton.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.bottom.equalTo(-18)
        }
        
        locationIcon.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 14, height: 14))
            make.left.equalTo(16.5)
            make.centerY.equalTo(likeButton)
        }
        
        locationLabel.snp.makeConstraints { make in
            make.left.equalTo(locationIcon.snp.right).offset(4)
            make.centerY.equalTo(likeButton)
        }
    }
    
    // MARK: - lazy load
    private lazy var cardView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 5
        view.clipsToBounds = false
        view.layerShadowColor = .rgba(0, 0, 0, 0.1)
        view.layerShadowOffset = CGSize(width: 0, height: 0)
        view.layerShadowOpacity = 1
        view.layerShadowRadius = 5
        return view
    }()
    
    private lazy var questionIcon: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "Community/question_icon"))
        return imageV
    }()
    
    private lazy var questionLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(16)
        label.textColor = .rgba(51, 51, 51)
        return label
    }()
    
    private lazy var answerCardView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(132, 97, 201, 0.02)
        view.layerBorderColor = .rgba(132, 97, 201, 0.41)
        view.layerBorderWidth = 0.5
        view.layerCornerRadius = 5
        return view
    }()
    
    private lazy var doctorHeadImageView: UIImageView = {
        let imageV = UIImageView()
        imageV.layerCornerRadius = 12.5
        return imageV
    }()
    
    private lazy var doctorInfoLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(44, 44, 44)
        return label
    }()
    
    private lazy var answerLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(14)
        label.textColor = .rgba(72, 68, 67)
        label.numberOfLines = 2
        return label
    }()
    
    private lazy var locationIcon: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "Community/location"))
        return imageV
    }()
    
    private lazy var locationLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(56, 52, 52)
        return label
    }()
    
    private lazy var likeButton: QMUIButton = {
        let button = QMUIButton(type: .custom)
        button.imagePosition = .left
        button.spacingBetweenImageAndTitle = 4.5
        button.titleLabel?.font = .regular(13)
        button.setTitleColor(.rgba(72, 68, 67), for: .normal)
        button.setTitle("0", for: .normal)
        button.setImage(UIImage(named: "Community/dislike"), for: .normal)
        button.addTarget(self, action: #selector(likeEvent), for: .touchUpInside)
        return button
    }()
}

extension MYGCommunityQuestionCell {
    @objc private func likeEvent() {
        likeAction?()
    }
}
